<?php

/*******************************
 MENUS SUPPORT
********************************/
if ( function_exists( 'wp_nav_menu' ) ){
	if (function_exists('add_theme_support')) {
		add_theme_support('nav-menus');
		add_action( 'init', 'register_my_menus' );
		function register_my_menus() {
			register_nav_menus(
				array(
					'main-menu' => __( 'Main Menu' )
				)
			);
		}
	}
}

/* CallBack functions for menus in case of earlier than 3.0 Wordpress version or if no menu is set yet*/

function primarymenu(){ ?>
			<div id="mainMenu" class="ddsmoothmenu">
				<ul>
					<?php wp_list_pages('title_li='); ?>
					<?php wp_list_categories('hide_empty=1&exclude=1&title_li='); ?>
				</ul>
			</div>
<?php }
/* Highlight current menu item*/
function wpse19375_nav_class( $classes, $item ){
	if( !in_array( 'current-menu-item', $classes ))
 	
		return $classes;
 		
	return $classes;
}

add_filter( 'nav_menu_css_class', 'wpse19375_nav_class', 10, 2 );
/* END - Highlight current menu item*/

/* BREADCRUMB - custom by JKey*/

function the_breadcrumb() {

  $delimiter = '&raquo;';
  $name = get_bloginfo('name'); //text for the 'Home' link
  $currentBefore = '<span class="current">';
  $currentAfter = '</span>';
 
  if ( !is_home() && !is_front_page() || is_paged() ) {
 
    echo '<div id="jk_breadcrumbs">';
 
    global $post;
    $home = get_bloginfo('url');
    echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';
 
    if ( is_category() ) {
      global $wp_query;
      $cat_obj = $wp_query->get_queried_object();
      $thisCat = $cat_obj->term_id;
      $thisCat = get_category($thisCat);
      $parentCat = get_category($thisCat->parent);
      if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
      echo $currentBefore;
      single_cat_title();
      echo $currentAfter;
 
    } elseif ( is_day() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('d') . $currentAfter;
 
    } elseif ( is_month() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('F') . $currentAfter;
 
    } elseif ( is_year() ) {
      echo $currentBefore . get_the_time('Y') . $currentAfter;
 
    } elseif ( is_single()) {
      if(get_post_type() == 'san-pham')
	  {
		  $terms = get_the_terms($id,'nhom-san-pham');
		  $arr = array();
		  $str = '';
		  $str .= '<a href="'.get_bloginfo('url').'/san-pham">Sản phẩm</a> &raquo; ';
		  foreach($terms as $term1)
		  {
			  if($term1->parent == 0)			  
			  {
				  $str .= '<a href="'.get_term_link($term1->name, 'nhom-san-pham').'" title="'.$term1->name.'">'.$term1->name.'</a> &raquo; ';
				  foreach($terms as $term2)
				  {
					  if($term2->parent == $term1->term_id)
					  {
						  $str .= '<a href="'.get_term_link($term2->name, 'nhom-san-pham').'" title="'.$term2->name.'">'.$term2->name.'</a> &raquo; ';
					  }
				  }
			  }		
		  }	  
		  echo $str;
		  the_title();
	  }
	  else
	  {	  
		  $cat = get_the_category(); $cat = $cat[0];
      	  echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
      	  echo $currentBefore;
     	  the_title();
     	  echo $currentAfter;
	  }
 
    } elseif ( is_page() && !$post->post_parent ) {
      echo $currentBefore;
      the_title();
      echo $currentAfter;
	  
 
    } elseif ( is_page() && $post->post_parent ) {
      $parent_id  = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
        $parent_id  = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
      echo $currentBefore;
      the_title();
      echo $currentAfter;
	
 
    } elseif ( is_search() ) {
      echo $currentBefore . 'Kết quả tìm kiếm &#39;' . get_search_query() . '&#39;' . $currentAfter;
 
    } elseif ( is_tag() ) {
      echo $currentBefore . 'tagged &#39;';
      single_tag_title();
      echo '&#39;' . $currentAfter;
 
    } elseif ( is_author() ) {
       global $author;
      $userdata = get_userdata($author);
      echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;
 
    } elseif ( is_404() ) {
      echo $currentBefore . 'Error 404' . $currentAfter;
   
   }  elseif (is_taxonomy('nhom-san-pham')) {
	     $str = '';
		 $str .= '<a href="'.get_bloginfo('url').'/san-pham">Sản phẩm</a> &raquo; ';
		 if(get_queried_object()->parent == 0)
		 	$str .= get_queried_object()->name;
		 else
		 {	
		 	$cur_term = get_the_terms('','nhom-san-pham');
			$parent_term_name = $cur_term[get_queried_object()->parent]->name; 
			$str .= '<a href="'.get_term_link($parent_term_name, 'nhom-san-pham').'" title="'.$parent_term_name.'">'.$parent_term_name.'</a> &raquo; '.get_queried_object()->name;
						
		 }
		echo $str; 
  	} 
    if ( get_query_var('paged') ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo __('Page') . ' ' . get_query_var('paged');
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';  
    }
 
    echo '</div>';
 
  }
}*/

/*function the_breadcrumb() {
	if (!is_home()) {
		echo '<div id="jk_breadcrumbs">';
		echo '<a href="';
		echo get_option('home');
		echo '">';
		bloginfo('name');
		echo "</a> » ";
		if (is_category() || is_single()) {
			the_category('title_li=');
			if (is_single()) {
				echo " » ";
				the_title();
			}
		} elseif (is_page()) {
			echo the_title();
		} 
	
	echo '</div>';
	}
}*/
/* END BREADCRUMB */ 


/*
	PHÂN TRANG
*/
//Start of Paganavi
/*function wp_corenavi() {
	global $wp_query, $wp_rewrite;
	$pages = '';
	$max = $wp_query->max_num_pages;
	if (!$current = get_query_var('paged')) $current = 1;
	$a['base'] = ($wp_rewrite->using_permalinks()) ? user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ) : @add_query_arg('paged','%#%');
	if( !empty($wp_query->query_vars['s']) ) $a['add_args'] = array( 's' => get_query_var( 's' ) );
	$a['total'] = $max;
	$a['current'] = $current;
	
	$total = 1; //1 - display the text "Page N of N", 0 - not display
	$a['mid_size'] = 5; //how many links to show on the left and right of the current
	$a['end_size'] = 1; //how many links to show in the beginning and end
	$a['prev_text'] = '&laquo; Previous'; //text of the "Previous page" link
	$a['next_text'] = 'Next &raquo;'; //text of the "Next page" link
	
	if ($max > 1) echo '<div class="pagenavi">';
	if ($total == 1 && $max > 1) $pages = '<span class="pages">Page ' . $current . ' of ' . $max . '</span>'."\r\n";
	echo $pages . paginate_links($a);
	if ($max > 1) echo '</div>';
}*
// END
/*******************************
 THUMBNAIL SUPPORT
********************************/

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 300, 200, true );

/* Get the thumb original image full url */

function get_thumb_urlfull ($postID) {
$image_id = get_post_thumbnail_id($post);  
$image_url = wp_get_attachment_image_src($image_id,'large');  
$image_url = $image_url[0]; 
return $image_url;
}

/*******************************
 EXCERPT LENGTH ADJUST
********************************/

function home_excerpt_length($length) {
	return 30;
}
add_filter('excerpt_length', 'home_excerpt_length');


/*******************************
 WIDGETS AREAS
********************************/

/*if ( function_exists('register_sidebar') )
register_sidebar(array(
	'name' => 'sidebar',
	'before_widget' => '<div class="rightBox">',
	'after_widget' => '</div>',
	'before_title' => '<h2>',
	'after_title' => '</h2>',
));

register_sidebar(array(
	'name' => 'footer',
	'before_widget' => '<div class="boxFooter">',
	'after_widget' => '</div>',
	'before_title' => '<h2>',
	'after_title' => '</h2>',
));*/
	
/*******************************
 PAGINATION
********************************/

function emm_paginate($args = null) {
	$defaults = array(
		'page' => null, 'pages' => null, 
		'range' => 3, 'gap' => 3, 'anchor' => 1,
		'before' => '<div class="emm-paginate">', 'after' => '</div>',
		'title' => __('Pages:'),
		'nextpage' => __('&raquo;'), 'previouspage' => __('&laquo'),
		'echo' => 1
	);

	$r = wp_parse_args($args, $defaults);
	extract($r, EXTR_SKIP);


    if (!$page && !$pages) {
		global $wp_query;

		$page = get_query_var('paged');
		$page = !empty($page) ? intval($page) : 1;

		$posts_per_page = intval(get_query_var('posts_per_page'));
		$pages = intval(ceil($wp_query->found_posts / $posts_per_page));
	}
	
	$output = "";
	if ($pages > 1) {	
		$output .= "$before<span class='emm-title'>$title</span>";
		$ellipsis = "<span class='emm-gap'>...</span>";

		if ($page > 1 && !empty($previouspage)) {
			$output .= "<a href='" . get_pagenum_link($page - 1) . "' class='emm-prev'>$previouspage</a>";
		}
		
		$min_links = $range * 2 + 1;
		$block_min = min($page - $range, $pages - $min_links);
		$block_high = max($page + $range, $min_links);
		$left_gap = (($block_min - $anchor - $gap) > 0) ? true : false;
		$right_gap = (($block_high + $anchor + $gap) < $pages) ? true : false;

		if ($left_gap && !$right_gap) {
			$output .= sprintf('%s%s%s', 
				emm_paginate_loop(1, $anchor), 
				$ellipsis, 
				emm_paginate_loop($block_min, $pages, $page)
			);
		}
		else if ($left_gap && $right_gap) {
			$output .= sprintf('%s%s%s%s%s', 
				emm_paginate_loop(1, $anchor), 
				$ellipsis, 
				emm_paginate_loop($block_min, $block_high, $page), 
				$ellipsis, 
				emm_paginate_loop(($pages - $anchor + 1), $pages)
			);
		}
		else if ($right_gap && !$left_gap) {
			$output .= sprintf('%s%s%s', 
				emm_paginate_loop(1, $block_high, $page),
				$ellipsis,
				emm_paginate_loop(($pages - $anchor + 1), $pages)
			);
		}
		else {
			$output .= emm_paginate_loop(1, $pages, $page);
		}

		if ($page < $pages && !empty($nextpage)) {
			$output .= "<a href='" . get_pagenum_link($page + 1) . "' class='emm-next'>$nextpage</a>";
		}

		$output .= $after;
	}

	if ($echo) {
		echo $output;
	}

	return $output;
}

function emm_paginate_loop($start, $max, $page = 0) {
	$output = "";
	for ($i = $start; $i <= $max; $i++) {
		$output .= ($page === intval($i)) 
			? "<span class='emm-page emm-current'>$i</span>" 
			: "<a href='" . get_pagenum_link($i) . "' class='emm-page'>$i</a>";
	}
	return $output;
}

function post_is_in_descendant_category( $cats, $_post = null )
{
	foreach ( (array) $cats as $cat ) {
		// get_term_children() accepts integer ID only
		$descendants = get_term_children( (int) $cat, 'category');
		if ( $descendants && in_category( $descendants, $_post ) )
			return true;
	}
	return false;
}

/*******************************
 CUSTOM COMMENTS
********************************/

function mytheme_comment($comment, $args, $depth) {
   $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
   	<div class="gravatar">
	 <?php echo get_avatar($comment,$size='50',$default=get_bloginfo('template_directory').'/images/default_ava.png' ); ?>
	 <div class="gravatar_mask"></div>
	</div>
     <div id="comment-<?php comment_ID(); ?>">
      <div class="text">
		  <?php comment_text() ?>
	  </div>
	  <div class="comment-meta commentmetadata clearfix">
	    <?php printf(__('<font style="font-weight:bold;font-size:12px">%s</font>'), get_comment_author_link()) ?><?php /*edit_comment_link(__('(Edit)'),'  ','')*/ ?> <span><?php echo date('d-m-Y',strtotime(get_comment_date())).' '.date('H:i',strtotime(get_comment_time())); ?>
	  </span>
	  </div>
	  
     
	  
	  <?php if ($comment->comment_approved == '0') : ?>
         <em><?php _e('Your comment is awaiting moderation.') ?></em>
         <br />
      <?php endif; ?>

    <!--  <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>-->
     </div>
<?php }

/*******************************
  THEME OPTIONS PAGE
********************************/

add_action('admin_menu', 'boldy_theme_page');
function boldy_theme_page ()
{
	if ( count($_POST) > 0 && isset($_POST['boldy_settings']) )
	{
		$options = array ('logo_img', 'logo_alt','contact_email','contact_text','cufon','linkedin_link','twitter_user','latest_tweet','facebook_link','keywords','description','analytics','copyright','home_box1','home_box1_link','home_box2','home_box2_link','home_box3','home_box3_link','blurb_enable','blurb_text','blurb_link','blurb_page', 'footer_actions','actions_hide','portfolio','blog','slider');
		
		foreach ( $options as $opt )
		{
			delete_option ( 'boldy_'.$opt, $_POST[$opt] );
			add_option ( 'boldy_'.$opt, $_POST[$opt] );	
		}			
		 
	}
	add_menu_page(__('LBM Theme Config'), __('LBM Theme'), 'edit_themes', basename(__FILE__), 'boldy_settings');
	add_submenu_page(__('LBM Options'), __('LBM Options'), 'edit_themes', basename(__FILE__), 'boldy_settings');
}
function boldy_settings()
{?>
<div class="wrap">
	<h2>LBM Theme Config</h2>
	
<form method="post" action="">

	<fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px;color:#2481C6; text-transform:uppercase;"><strong>General Settings</strong></legend>
	<table class="form-table">
		<!-- General settings -->
		
		<tr valign="top">
			<th scope="row"><label for="logo_img">Change logo</label></th>
			<td>
				<input name="logo_img" type="text" id="logo_img" value="<?php echo get_option('boldy_logo_img'); ?>" class="regular-text" /><br />
				<em>current logo:</em> <br /> <img src="<?php bloginfo('template_directory'); echo get_option('boldy_logo_img'); ?>" alt="<?php echo get_option('boldy_logo_alt'); ?>" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="logo_alt">Logo ALT Text</label></th>
			<td>
				<input name="logo_alt" type="text" id="logo_alt" value="<?php echo get_option('boldy_logo_alt'); ?>" class="regular-text" />
			</td>
		</tr>
        
		 <tr valign="top">
			<th scope="row"><label for="cufon">Cufon Font Replacement</label></th>
			<td>
				<select name="cufon" id="cufon">
					<option value="yes" <?php if(get_option('boldy_cufon') == 'yes'){?>selected="selected"<?php }?>>Yes</option>		
					<option value="no" <?php if(get_option('boldy_cufon') == 'no'){?>selected="selected"<?php }?>>No</option>
				</select>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="b">Choose Blog Category</label></th>
			<td>
				<?php wp_dropdown_categories("name=blog&hide_empty=0&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_blog')); ?>
			</td>
		</tr>
		 <tr valign="top">
			<th scope="row"><label for="portfolio">Choose Portfolio Category</label></th>
			<td>
				<?php wp_dropdown_categories("name=portfolio&hide_empty=0&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_portfolio')); ?>
			</td>
		</tr>
	</table>
	</fieldset>
	
	<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="boldy_settings" value="save" style="display:none;" />
		</p>
	
	<fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>Social Links</strong></legend>
		<table class="form-table">
		<tr valign="top">
			<th scope="row"><label for="twitter_user">Twitter Username</label></th>
			<td>
				<input name="twitter_user" type="text" id="twitter_user" value="<?php echo get_option('boldy_twitter_user'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="latest_tweet">Display Latest Tweet</label></th>
			<td>
				<select name="latest_tweet" id="latest_tweet">		
					<option value="yes" <?php if(get_option('boldy_latest_tweet') == 'yes'){?>selected="selected"<?php }?>>Yes</option>
                    <option value="no" <?php if(get_option('boldy_latest_tweet') == 'no'){?>selected="selected"<?php }?>>No</option>
				</select>
			</td>
		</tr>
        <tr valign="top">
			<th scope="row"><label for="facebook_link">Facebook link</label></th>
			<td>
				<input name="facebook_link" type="text" id="facebook_link" value="<?php echo get_option('boldy_facebook_link'); ?>" class="regular-text" />
			</td>
		</tr>
        <tr valign="top">
			<th scope="row"><label for="flickr_link">LinkedIn link</label></th>
			<td>
				<input name="linkedin_link" type="text" id="linkedin_link" value="<?php echo get_option('boldy_linkedin_link'); ?>" class="regular-text" />
			</td>
		</tr>
        </table>
        </fieldset>
		<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="boldy_settings" value="save" style="display:none;" />
		</p>
		
		<fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px;color:#2481C6; text-transform:uppercase;"><strong>Homepage Settings</strong></legend>
	<table class="form-table">
		<!-- Homepage Boxes 1 -->
		<tr>
			<th colspan="2"><strong>Homepage Slider </strong></th>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="slider">Homepage Slider Images Page</label></th>
			<td>
				<?php wp_dropdown_pages("name=slider&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_slider')); ?>
			</td>
		</tr>
		<tr>
			<th colspan="2"><strong>Homepage Boxes </strong></th>
		</tr>
		<tr>
			<th colspan="2"> They should be ALL selected ! Other way the row wont appear at all.</th>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="home_box1">Home Box1 Page</label></th>
			<td>
				<?php wp_dropdown_pages("name=home_box1&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_home_box1')); ?>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="home_box1_link">Home Box1 "read more" link</label></th>
			<td>
				<input name="home_box1_link" type="text" id="home_box1_link" value="<?php echo get_option('boldy_home_box1_link'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="home_box2">Homepage Box2 Page</label></th>
			<td>
				<?php wp_dropdown_pages("name=home_box2&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_home_box2')); ?>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="home_box2_link">Home Box2 "read more" link</label></th>
			<td>
				<input name="home_box2_link" type="text" id="home_box2_link" value="<?php echo get_option('boldy_home_box2_link'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="home_box3">Home Box3 Page</label></th>
			<td>
				<?php wp_dropdown_pages("name=home_box3&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_home_box3')); ?>
			</td>
		</tr>	
		<tr valign="top">
			<th scope="row"><label for="home_box3_link">Home Box3 "read more" link</label></th>
			<td>
				<input name="home_box3_link" type="text" id="home_box3_link" value="<?php echo get_option('boldy_home_box3_link'); ?>" class="regular-text" />
			</td>
		</tr>
		<tr>
			<th colspan="2"><strong>Homepage Blurb (request quote section) </strong></th>
		</tr>
		 <tr>
			<th><label for="blurb_enable">Display Homepage Blurb</label></th>
			<td>
				<select name="blurb_enable" id="blurb_enable"> 
					<option value="yes" <?php if(get_option('boldy_blurb_enable') == 'yes'){?>selected="selected"<?php }?>>Yes</option>		
					<option value="no" <?php if(get_option('boldy_blurb_enable') == 'no'){?>selected="selected"<?php }?>>No</option>
				</select><br />
                <em>If "Yes" is selected and text field is empty, the blurb wont appear</em>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="blurb_text">Blurb Text</label></th>
			<td>
				<textarea name="blurb_text" id="blurb_text" rows="3" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('boldy_blurb_text')); ?></textarea>
			</td>
		</tr>
		<tr valign="top">
			<th scope="row"><label for="blurb_link">Request Quote Link</label></th>
			<td>
				<input name="blurb_link" type="text" id="blurb_link" value="<?php echo get_option('boldy_blurb_link'); ?>" class="regular-text" />
				<br />
				<em>You can either enter a link manually or select a page to point at.</em>
			</td>
		</tr>
		
		<tr valign="top">
			<th scope="row"><label for="blurb_page">Request Quote Page</label></th>
			<td>
				<?php wp_dropdown_pages("name=blurb_page&show_option_none=".__('- Select -')."&selected=" .get_option('boldy_blurb_page')); ?>
				<br />
				<em>You can either enter a link manually or select a page to point at.</em>
			</td>
		</tr>
	</table>
	</fieldset>
	<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="boldy_settings" value="save" style="display:none;" />
		</p>
	
    <fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>Contact Page Settings</strong></legend>
		<table class="form-table">	
        <tr>
        	<td colspan="2"></td>
        </tr>
         <tr valign="top">
			<th scope="row"><label for="contact_text">Contact Page Text</label></th>
			<td>
				<textarea name="contact_text" id="contact_text" rows="7" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('boldy_contact_text')); ?></textarea>
			</td>
		</tr>
        <tr valign="top">
			<th scope="row"><label for="contact_email">Email Address for Contact Form</label></th>
			<td>
				<input name="contact_email" type="text" id="contact_email" value="<?php echo get_option('boldy_contact_email'); ?>" class="regular-text" />
			</td>
		</tr>
        </table>
     </fieldset>
	 <p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="boldy_settings" value="save" style="display:none;" />
	</p>
	
	<fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>Footer</strong></legend>
		<table class="form-table">
		<tr>
			<th colspan="2"><strong>Footer Twitter &amp; Quick Contact </strong></th>
		</tr>
		<tr>
			<th><label for="footer_actions">Display Footer Twitter &amp; Quick Contact Section</label></th>
			<td>
				<select name="footer_actions" id="footer_actions"> 
					<option value="yes" <?php if(get_option('boldy_footer_actions') == 'yes'){?>selected="selected"<?php }?>>Yes</option>		
					<option value="no" <?php if(get_option('boldy_footer_actions') == 'no'){?>selected="selected"<?php }?>>No</option>
				</select>
			</td>
		</tr>
		<tr>
			<th><label for="actions_hide">Default Section Visibility</label></th>
			<td>
				<select name="actions_hide" id="actions_hide"> 
					<option value="visible" <?php if(get_option('boldy_actions_hide') == 'visible'){?>selected="selected"<?php }?>>Visible</option>		
					<option value="hidden" <?php if(get_option('boldy_actions_hide') == 'hidden'){?>selected="selected"<?php }?>>Hidden</option>
				</select>
			</td>
		</tr>
		<tr>
			<th colspan="2"><strong>Copyright Info</strong></th>
		</tr>
        <tr>
			<th><label for="copyright">Copyright Text</label></th>
			<td>
				<textarea name="copyright" id="copyright" rows="4" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('boldy_copyright')); ?></textarea><br />
				<em>You can use HTML for links etc.</em>
			</td>
		</tr>
		
		
	</table>
	</fieldset>
	<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="boldy_settings" value="save" style="display:none;" />
	</p>
        
      <fieldset style="border:1px solid #ddd; padding-bottom:20px; margin-top:20px;">
	<legend style="margin-left:5px; padding:0 5px; color:#2481C6;text-transform:uppercase;"><strong>SEO</strong></legend>
		<table class="form-table">
        <tr>
			<th><label for="keywords">Meta Keywords</label></th>
			<td>
				<textarea name="keywords" id="keywords" rows="7" cols="70" style="font-size:11px;"><?php echo get_option('boldy_keywords'); ?></textarea><br />
                <em>Keywords comma separated</em>
			</td>
		</tr>
        <tr>
			<th><label for="description">Meta Description</label></th>
			<td>
				<textarea name="description" id="description" rows="7" cols="70" style="font-size:11px;"><?php echo get_option('boldy_description'); ?></textarea>
			</td>
		</tr>
		<tr>
			<th><label for="ads">Google Analytics code:</label></th>
			<td>
				<textarea name="analytics" id="analytics" rows="7" cols="70" style="font-size:11px;"><?php echo stripslashes(get_option('boldy_analytics')); ?></textarea>
			</td>
		</tr>
		
	</table>
	</fieldset>
	<p class="submit">
		<input type="submit" name="Submit" class="button-primary" value="Save Changes" />
		<input type="hidden" name="boldy_settings" value="save" style="display:none;" />
	</p>
</form>
</div>
<?php }
/*******************************
  CONTACT FORM 
********************************/

 function hexstr($hexstr) {
  $hexstr = str_replace(' ', '', $hexstr);
  $hexstr = str_replace('\x', '', $hexstr);
  $retstr = pack('H*', $hexstr);
  return $retstr;
}

function strhex($string) {
  $hexstr = unpack('H*', $string);
  return array_shift($hexstr);
}
#
# Group menu footer - custom by JKey
#
add_action('admin_menu', footer_menu);
function footer_menu()
{
	global $wpdb;
	if(count($_POST) > 0 && isset($_POST['sm_update_footermenu']))
	{
		$num_row = count($_POST['footer_menu_id']);
		for($i = 0; $i < $num_row; $i++)
		{
			$id = $_POST['footer_menu_id'][$i];
			$sql = "UPDATE lbm_footer_menu
					SET name = '".$_POST['name_menu'][$id]."',
						link_menu = '".$_POST['link_menu'][$id]."',
						parent_menu = '".$_POST['parent_menu'][$id]."',
						priority = '".$_POST['priority'][$id]."',
						post_status = '".$_POST['post_status'][$id]."'
					WHERE id = '".$id."'";
			$wpdb->query($sql);			
		}	

	}
	
	if(isset($_POST['new_footermenu_name']) && !empty($_POST['new_footermenu_name']))
	{
		$sql_ins = "INSERT INTO lbm_footer_menu (`id` ,
												`name` ,
												`link_menu` ,
												`deleted` ,
												`parent_menu` ,
												`priority` ,
												`post_status`
												)
												VALUES (NULL , '".trim($_POST['new_footermenu_name'])."', '#', '', '', '1', 'inactived')";
		$wpdb->query($sql_ins);
		
	}
	add_menu_page('Footer Menu', 'Footer Menu', 'edit_themes', 'footer-menu', 'manage_footer_menu', 'images/editmenu.png');//images/menu-edit-icon.png
}

function manage_footer_menu()
{
?>
	<script type="text/javascript" src="<?php bloginfo('template_directory') ?>/custom_js/admin_jk.js" ></script>
<?

	
	global $wpdb;
	$sql = "SELECT id,
				   name,
				   link_menu,
				   parent_menu,
				   priority,
				   post_status
			FROM lbm_footer_menu
			WHERE deleted = 0
			ORDER BY parent_menu, priority, name ASC";
	$res = $wpdb->get_results($sql);
	$html = '';
	$html .= '<div class="wrap">
				<div class="icon32" style="background-image:url(images/editmenu32.png)"></div>
				<h2>Footer Menu</h2>';
	$html .= '<br />';	
	$html .= '<form action="" method="post" name="frm_add_footermenu">
				<input type="submit" name="sm_add_footermenu" id="sm_add_footermenu" class="button-primary" value="Thêm mới">
				<input type="text" name="new_footermenu_name" size=40 />				
			</form>';	
	$html .= '<br />';		
	$html .= '<form action="" method="post" name="frm_update_footermenu">';
	$html .= '<table class="wp-list-table widefat fixed pages" cellspacing="0">';
	$html .= '<thead>
				<tr>
					<th id="cb" class="manage-column column-cb check-column" ><input type="checkbox" /></th>
					<th width="5%" style="text-align:center">STT</th>
					<th width="18%">Tên</th>					
					<th width="15%">Menu cấp cha</th>
					<th width="10%">Độ ưu tiên</th>
					<th width="30%">Liên kết</th>
					<th>Trạng thái</th>
				</tr>
			   </thead>';
	$html .= '<tbody>';
	$i = 1;

	foreach($res as $row)
	{		
		if($row->parent_menu != 0)
		{
			$link = '<input type="text" name="link_menu['.$row->id.']" value="'.$row->link_menu.'"/>';
			$res2 = $wpdb->get_results("SELECT id FROM lbm_footer_menu WHERE id = '".$row->parent_menu."'");
			foreach($res2 as $row2)
			{
				$parent_menu = $row2->id;
				
			}
		}
		else 
		{
			$link = '';
		}
		$html .= '<tr height="30px" style="'.($row->post_status == 'inactived' ? 'background-color:#FACE88' : '').'">
					<td><input type="checkbox" />
						<input type="hidden" name="footer_menu_id[]" value="'.$row->id.'" />
					</td>
				  	<td style="text-align:center">'.$i.'</td>
					<td><input type="text" readonly="readonly" name="name_menu['.$row->id.']" id="name_menu'.$row->id.'" value="'.$row->name.'" />&nbsp;<img src="images/edit.png" onclick="edit_footer_menu('.$row->id.')" alt="Chỉnh sửa" title="Chỉnh sửa"  />&nbsp;<img src="images/delete.png" onclick="" alt="Xóa" title="Xóa" /></td>					
					<td>
						<select name="parent_menu['.$row->id.']">
							<option value=0>--</option>
							'.returnParent_Menu($parent_menu).'
						</select>
					</td>
					<td>
						<select name="priority['.$row->id.']">
							'.returnPriority($row->priority).'
						</select></td>
					<td>'.$link.'</td>
					<td><select name="post_status['.$row->id.']">
							<option '.($row->post_status == 'published' ? 'selected="selected"' : '').' value="published" >Published</option>
							<option '.($row->post_status == 'inactived' ? 'selected="selected"' : '').' value="inactived" >Inactive</option>
						</select>					
					</td>
				 </tr>';
		$i++;
		
	}
	
	$html .= '</tbody>';
	$html .= '</table>';
	$html .= '<p class="submit">
				<input type="submit" name="sm_update_footermenu" id="sm_update_footermenu" class="button-primary" value="Lưu thay đổi">
			  </p>'; 
	$html .= '</form>';
	$html .= '</div>';		  
	echo $html;
}

function returnParent_Menu($val)
{
	global $wpdb;
	$res = $wpdb->get_results("SELECT id, name FROM lbm_footer_menu WHERE deleted = 0 AND parent_menu = '0'");
	$html = '';
	foreach($res as $row)
	{
		if($val == $row->id) $selected = 'selected="selected"'; else $selected = '';
		$html .= '<option '.$selected.' value="'.$row->id.'" >'.$row->name.'</option>';
	}
	
	return $html;
}
function returnPriority($val)
{
	$html .= '';
	for($i = 1; $i < 10; $i++)
	{
		if($i == $val) $selected = 'selected="selected"'; else $selected = '';
		$html .= '<option '.$selected.' value="'.$i.'" >'.$i.'</option>';
	}
	
	return $html;
}
/* FORMAT NUMBER - BY JKEY */
function format_number($num, $dec_place)
{
	$number = number_format($num,$dec_place,',','.');
	return $number;
}
/* END FORMAT NUMBER - BY JKEY */
/*
	CUSTOM BY JKEY
*/
	require(TEMPLATEPATH . '/includes/custom_post.php' );
	require(TEMPLATEPATH . '/includes/custom_taxonomy.php' );
	require(TEMPLATEPATH . '/includes/meta_box.php');
	require(TEMPLATEPATH . '/includes/custom_fields.php');
	require(TEMPLATEPATH . '/includes/custom_permalinks.php');
	require(TEMPLATEPATH . '/includes/custom_widget.php');


	
?>

